package com.ccp.dev.system.controller;

import com.alibaba.fastjson.JSON;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.Convert;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.GlobalType;
import com.ccp.dev.system.model.IndexColumn;
import com.ccp.dev.system.service.GlobalTypeService;
import com.ccp.dev.system.service.IndexColumnService;
import com.ccp.dev.system.service.SysBpmFormQueryService;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 首页栏目 信息操作处理
 *
 * @author doufangzheng
 * @date 2019-03-19
 */
@Controller
@Action(ownermodel = SysAuditModelType.HOME_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/indexColumn")
public class IndexColumnController extends SysBaseController {

    @Autowired
    private IndexColumnService indexColumnService;

    @Autowired
    private GlobalTypeService globalTypeService;

    @Autowired
    private SysBpmFormQueryService sysBpmFormQueryService;

    /**
     * 查询首页栏目列表
     */
    @GetMapping("/list")
    @ResponseBody
    @Action(description="查询首页栏目",detail="查询首页栏目<#if success>成功<#else>失败</#if>",exectype = "查询日志")
    public ResultData list(HttpServletRequest request) {
        try{
            SysAuditThreadLocalHolder.putParamerter("success", true);
            QueryFilter queryFilter = new QueryFilter(request);
            List<IndexColumn> list = indexColumnService.queryIndexColumnList(queryFilter);
            return getResultDataSuccess("operation.success",queryFilter.getPageBean().getTotalCount(),list);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }
    /**
     * 新增保存首页栏目
     */
    @PostMapping("/add")
    @ResponseBody
    @Action(description="保存首页栏目",detail="<#if StringUtil.isEmpty(id)>新增<#else>更新</#if>布局【${name}】<#if success>成功<#else>失败</#if>",exectype = "编辑日志")
    public ResultData save(IndexColumn indexColumn) {
        indexColumn.setDataParam(StringEscapeUtils.unescapeHtml4(indexColumn.getDataParam()));
        indexColumn.setTemplateHtml(StringEscapeUtils.unescapeHtml4(indexColumn.getTemplateHtml()));
        try {
            SysAuditThreadLocalHolder.putParamerter("success", true);
            SysAuditThreadLocalHolder.putParamerter("id", indexColumn.getId());
            SysAuditThreadLocalHolder.putParamerter("name", indexColumn.getName());
            if(indexColumnService.getIndexColumnByAliasAndId(indexColumn.getAlias(),indexColumn.getId())){
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError("indexColumn.alias.isExist");
            }
            indexColumnService.saveIndexColumn(indexColumn);
            return getResultDataSuccess("save.success");
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("save.failure");
        }
    }

    /**
     * 查看栏目
     * @param request 请求
     * @return ResultData
     */
    @GetMapping("/get")
    @ResponseBody
    @Action(description="查看首页栏目",detail="查看首页栏目<#if success>【${name}】成功<#else>失败</#if>",exectype = "查询日志")
    public ResultData getIndexColumn(HttpServletRequest request){
        try{
            SysAuditThreadLocalHolder.putParamerter("success", true);
            String id = RequestUtil.getString(request,"id");
            IndexColumn indexColumn = indexColumnService.getIndexColumnById(id);
            SysAuditThreadLocalHolder.putParamerter("name", indexColumn.getName());
            return getResultDataSuccess("operation.success",indexColumn);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }
    /**
     * 新增保存首页栏目
     */
    @GetMapping("/del")
    @ResponseBody
    @Action(description="删除首页栏目",detail="删除首页栏目<#if success><#list name as item>【${item}】</#list>成功<#else>失败</#if>",exectype = "删除日志")
    public ResultData del(HttpServletRequest request){
        String ids = RequestUtil.getString(request,"ids");
        SysAuditThreadLocalHolder.putParamerter("name",indexColumnService.queryIndexColumnNameByIds(Convert.toStrArray(ids)));
        try{
            indexColumnService.deleteIndexColumnByIds(ids);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("delete.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("delete.failure");
        }
    }
    /*-------------------------一以下是页面跳转------------------------*/

    /**
     * 跳转到首页栏目列表页
     * @return html文件前缀
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/list")
    @Action(description="首页栏目列表页面跳转",exectype = "页面跳转日志")
    public String viewIndexColumnList(){
        return "sysIndexConfig/indexColumn/list";
    }
    /**
     * 跳转到首页栏目添加页
     * @return html文件前缀
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/add")
    @Action(description="首页栏目新增页面跳转",exectype = "页面跳转日志")
    public String viewIndexColumnAdd(HttpServletRequest request,Model model){
        String id=RequestUtil.getString(request,"id");
        if(StringUtil.isNotEmpty(id)){
            model.addAttribute("indexColumn",indexColumnService.getIndexColumnById(id));
            model.addAttribute("id",id);
        }
        List<GlobalType> catalogList = globalTypeService.getByCatKey(GlobalType.CAT_INDEX_COLUMN, false);
        model.addAttribute("catalogList",catalogList);
        model.addAttribute("dataFromQueryList", JSON.toJSONString(sysBpmFormQueryService.queryList(null)));
        return "sysIndexConfig/indexColumn/addEdit";
    }
    /**
     * 跳转到首页栏目查看页
     * @return html文件前缀
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/view")
    @Action(description="首页栏目查看页面跳转",exectype = "页面跳转日志")
    public String viewIndexColumnView(HttpServletRequest request,Model model){
        String id=RequestUtil.getString(request,"id");
        model.addAttribute("id",id);
        model.addAttribute("indexColumn",indexColumnService.getIndexColumnById(id));
        List<GlobalType> catalogList = globalTypeService.getByCatKey(GlobalType.CAT_INDEX_COLUMN, false);
        model.addAttribute("catalogList",catalogList);
        model.addAttribute("dataFromQueryList",sysBpmFormQueryService.queryList(null));
        return "sysIndexConfig/indexColumn/view";
    }
}
